﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPMS.Utility;
using SPMS.Commons.Configuration;

namespace SPMS.Log.Configuration.Handler
{
    /// <summary>
    /// 属性节点处理器
    /// </summary>
    /// <remarks>用于处理属性节点</remarks>
    /// <history>
    /// [zanezeng]               2009/10/15 14:57    创建
    /// </history>
    public sealed class Property_NodeHandler : AbstractNodeHandler
    {
        /// <summary>
        /// 处理指定的节点
        /// </summary>
        /// <param name="node">配置节点</param>
        /// <param name="context">处理上下文</param>
        /// <returns>根据配置信息创建的对象</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-11-17 11:39    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/15 14:57    创建
        /// </history>
        protected override object ProcessInternal( IConfigurationNode node, ParseContext context )
        {
            //输出日志
            if (LoggingContext.MetaLog.IsTraceEnabled)
            {
                LoggingContext.MetaLog.Trace( "开始解析属性" );
            }

            //获得属性的名称
            string propertyName = this.GetAttributeValue( node, LogConfiguationTags.ATTRIBUTE_NAME, context );

            //判断是否指定了属性名称
            if (string.IsNullOrEmpty( propertyName ))
            {
                //抛出异常
                throw new ConfigurationException( string.Format( Properties.Resources.MSG_CONFIGURATION_ATTRIBUTE_REQUIRED, node.Name, LogConfiguationTags.ATTRIBUTE_NAME ), node );
            }

            //用于保存属性类型
            Type propertyType = null;

            //获得属性的类型类型
            string typeName = this.GetAttributeValue( node, LogConfiguationTags.ATTRIBUTE_TYPE, context );

            //判断是否设置了属性类型
            if (!string.IsNullOrEmpty( typeName ))
            {
                //解析属性类型
                propertyType = Type.GetType( typeName );
            }

            //获得属性的值
            string value = this.GetAttributeValue( node, LogConfiguationTags.ATTRIBUTE_VALUE, context );

            //用于保存属性值
            object propertyValue = value;

            //判断是否设定了属性的值
            if (!string.IsNullOrEmpty( value ))
            {
                //判断是否指定了属性类型
                if (null != propertyType)
                {
                    //尝试进行类型转化
                    propertyValue = ObjectUtility.ConvertTo( value, propertyType );
                }

                //输出日志
                if (LoggingContext.MetaLog.IsTraceEnabled)
                {
                    LoggingContext.MetaLog.Trace( "属性'{0}'='{1}'解析完毕", propertyName, propertyValue );
                }

                //返回属性值
                return new PropertyValue( propertyName, propertyValue );
            }

            //判断是否设置了属性类型
            if (null == propertyType)
            {
                //抛出异常
                throw new ConfigurationException( string.Format( Properties.Resources.MSG_CONFIGURATION_ATTRIBUTE_REQUIRED, node.Name, LogConfiguationTags.ATTRIBUTE_TYPE ), node );
            }

            //获得属性值
            propertyValue = Activator.CreateInstance( propertyType );

            //用于保存配置节点处理器
            IConfigurationNodeHandler<ParseContext> handler;

            //用于保存处理的结果
            object instance = null;

            //用于保存属性
            PropertyValue property = null;

            //用于保存属性
            System.Reflection.PropertyInfo propertyInfo = null;

            //循环当前配置节点的所有子节点
            foreach (IConfigurationNode subNode in node.ChildNodes)
            {
                //获得配置节点处理器
                handler = context.GetNodeHandler( subNode );

                //判断是否为Null
                if (null == handler)
                {
                    //抛出异常
                    throw new NotSupportedException( string.Format( Properties.Resources.MSG_TAG_NOTSUPPORTED, subNode.Name ) );
                }

                //处理节点
                instance = handler.Porcess( subNode, context );

                //判断是否为属性
                if (subNode.Name == LogConfiguationTags.TAG_PROPERTY)
                {
                    //转化为属性
                    property = (PropertyValue)instance;

                    //获得属性
                    propertyInfo = propertyType.GetProperty( property.Name );

                    //设置属性值
                    propertyInfo.SetValue( propertyValue, ObjectUtility.ConvertTo( property.Value, propertyInfo.PropertyType ), null );
                }
            }

            //输出日志
            if (LoggingContext.MetaLog.IsTraceEnabled)
            {
                LoggingContext.MetaLog.Trace( "属性'{0}'='{1}'解析完毕", propertyName, propertyValue );
            }

            //返回属性值
            return new PropertyValue( propertyName, propertyValue );
        }
    }
}
